package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.dto.base.EamSopTypeDTO;
import com.jcfk.eam.domain.query.base.EamSopTypeQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.base.EamSopTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 标准作业类型
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-08
 */
@Api(tags = {"标准作业类型管理"})
@RestController
@RequestMapping("eamsoptype")
public class EamSopTypeController {
    @Autowired
    private EamSopTypeService eamSopTypeService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamSopTypeQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询标准作业类型")
    @PostMapping("/queryByPage")
    public RetPage<EamSopTypeDTO> queryByPage(@RequestBody ReqPageParam<EamSopTypeQuery> requestParam) {

        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<EamSopTypeQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        DataPage<EamSopTypeDTO> page = new DataPage<EamSopTypeDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamSopTypeDTO.class);
        page = eamSopTypeService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 标准作业类型
     * 根据id 查询详情
     */
    @ApiOperation("查询标准作业类型")
    @GetMapping("/get/{id}")
    public RetResult<EamSopTypeDTO> get(@PathVariable("id") String id) {
        EamSopTypeDTO item = eamSopTypeService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理标准作业类型
     */
    @ApiOperation("保存标准作业类型")
    @PostMapping("/save")
    @RequestLogHeader(modular = "标准作业类型管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamSopTypeDTO eamSopType) {
        // 数据校验
        RetResult ret = eamSopTypeService.isValid(eamSopType);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamSopType.getId())) {
            // 新增
            eamSopTypeService.insert(eamSopType);
        } else {
            // 更新
            eamSopTypeService.update(eamSopType);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除标准作业类型
     */
    @ApiOperation("批量删除标准作业类型")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "标准作业类型管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamSopTypeDTO eamSopType) {
        return RetResult.ok(eamSopTypeService.logicDeleteList(eamSopType));
    }


    /**
     * 删除标准作业类型
     */
    @ApiOperation("删除标准作业类型")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "标准作业类型管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamSopTypeService.deleteItem(id);
        return RetResult.ok();
    }

    /**
     * 删除标准作业类型
     */
    @ApiOperation("列表")
    @PostMapping("/getlist")
    @RequestLogHeader(modular = "标准作业类型管理", businessType = "列表")
    public RetResult<?> getList(@RequestBody EamSopTypeQuery query) {
        query.setEnabled(1);
        query.setDeleted(0);
        List<EamSopTypeDTO> list = eamSopTypeService.getList(query);
        return RetResult.ok(list);
    }
}
